#include "stm32f10x.h"                  // Device header

void IC_Init(void)
{
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);//使能APB2时钟
	
	GPIO_InitTypeDef GPIO_InitStructure;//设定GPIO结构体
	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0;//Pin
	GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(GPIOB,&GPIO_InitStructure);//将设置的结构体的值传入
	
	TIM_InternalClockConfig(TIM3);
	
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;//第三步时基单元初始化
	TIM_TimeBaseInitStruct.TIM_ClockDivision=TIM_CKD_DIV1;
	TIM_TimeBaseInitStruct.TIM_CounterMode=TIM_CounterMode_Up;//向上计数
	TIM_TimeBaseInitStruct.TIM_Period=65536-1;//周期,ARR重装计数器的值，取值0~65535
	TIM_TimeBaseInitStruct.TIM_Prescaler=72-1;//PSC预分频器的值,因为预分频器和计数器都有一个数的偏差，所以-1
	TIM_TimeBaseInitStruct.TIM_RepetitionCounter=0;//重复计数器的值，高级计数器才有
	TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStruct);
	
	TIM_ICInitTypeDef TIM_ICInitStruct;//输入捕获
	TIM_ICInitStruct.TIM_Channel=TIM_Channel_3;//通道3
	TIM_ICInitStruct.TIM_ICFilter=0xF;//滤波
	TIM_ICInitStruct.TIM_ICPolarity=TIM_ICPolarity_Falling;//
	TIM_ICInitStruct.TIM_ICPrescaler=TIM_ICPSC_DIV1;//不分频
	TIM_ICInitStruct.TIM_ICSelection=TIM_ICSelection_DirectTI;//直连通道
	TIM_ICInit(TIM3,&TIM_ICInitStruct);
	
	TIM_SelectInputTrigger(TIM3,TIM_TS_TI1FP1);//触发源
	TIM_SelectSlaveMode(TIM3,TIM_SlaveMode_Reset);//选择从模式,cy
	
	TIM_Cmd(TIM3,ENABLE);
}

uint32_t IC_Get(void)
{
	return 1000000/TIM_GetCapture1(TIM3);
}
